package roborally;


/**
* An enum Orientation, that holds all possible orientations(TOP,RIGHT,BOTTOM,LEFT).
* 
* @version  2.90
* @author   Michiel Ruelens & Dieter Naten
*/
public enum Orientation {
	
	TOP, RIGHT, BOTTOM, LEFT;
	
	
	/**
	 * This method returns the next orientation when turning clockwise.
	 * 
	 * @param 	orientation
	 * 			The orientation that is being turned from.
	 * @return 	The next orientation when turning Clockwise. Returns TOP if the given argument was null.
	 * 			| if(orientation == TOP) --> result == RIGHT
	 * 			| if(orientation == RIGHT) --> result == BOTTOM
	 * 			| if(orientation == BOTTOM) --> result == LEFT
	 * 			| if(orientation == LEFT) --> result == TOP		
	 */
	public static Orientation getNextOrientationClockwise(Orientation orientation){
		Orientation result = TOP;
		if(orientation!=null){
			if(orientation == TOP){
				result = RIGHT;
			}
			else if(orientation == RIGHT){
				result = BOTTOM;
			}
			else if(orientation == BOTTOM){
				result = LEFT;
			}
		}
		else{
			System.out.println("The orientation cannot be null");
		}
		return result;
	}
	
	/**
	 * This method returns the next orientation when turning anticlockwise.
	 * 
	 * @param 	orientation
	 * 			The orientation that is being turned from.
	 * @return 	The next orientation when turning Clockwise. Returns TOP if the given argument was null.
	 * 			| if(orientation == TOP) --> result == LEFT
	 * 			| if(orientation == RIGHT) --> result == TOP
	 * 			| if(orientation == BOTTOM) --> result == RIGHT
	 * 			| if(orientation == LEFT) --> result == BOTTOM	
	 */
	public static Orientation getNextOrientationAntiClockwise(Orientation orientation){
		Orientation result = TOP;
		if(orientation!=null){
			if(orientation == TOP){
				result = LEFT;
			}
			else if(orientation == BOTTOM){
				result = RIGHT;
			}
			else if(orientation == LEFT){
				result = BOTTOM;
			}
		}
		else{
			System.out.println("The orientation cannot be null");
		}
		return result;
	}
	
	/**
	 * Converts the given integer into an orientation.
	 * 
	 * @param	i
	 * 			| The given integer that is being converted.
	 * @return	|	if(i == 0) --> result == TOP
	 *			|	if(i == 1) --> result == RIGHT
	 *			|	if(i == 2) --> result == BOTTOM
	 *			|	if(i == 3) --> result == LEFT
	 */
	public static Orientation convertIntToOrientation(int i){
		Orientation result = null;
		if(0<=i && i<=3){
			if(i == 0){
				result = TOP;
			}
			else if(i == 1){
				result = RIGHT;
			}
			else if(i == 2){
				result = BOTTOM;
			}
			else if(i == 3){
				result = LEFT;
			}
		}
		else{
			System.out.println("The interger for the orientation has to be 0,1,2 or 3, not " + i);
		}
		return result;
	}
	
	/**
	 * Converts the given orientation into an integer.
	 * 
	 * @param	orientation
	 * 			| The given orientation that is being converted.
	 * @return	|	if(orientation == TOP) --> result == 0
	 *			|	if(orientation == RIGHT) --> result == 1
	 *			|	if(orientation == BOTTOM) --> result == 2
	 *			|	if(orientation == LEFT) --> result == 3
	 */
	public static int convertOrientationToInt(Orientation orientation){
		int result = -1;
		if(orientation!=null){
			if(orientation == TOP){
				result = 0;
			}
			if(orientation == RIGHT){
				result = 1;
			}
			if(orientation == BOTTOM){
				result = 2;
			}
			if(orientation == LEFT){
				result = 3;
			}
		}
		else{
			System.out.println("The orientation cannot be null");
		}
		return result;
	}
	
	/**
	 * This method verifies if the given orientation is valid.
	 * 
	 * @param 	orientation
	 * 			The orientation that is being checked.
	 * @return	| (orientation instanceof Orientation)
	 */
	public static boolean isValidOrientation(Orientation orientation){
		return (orientation instanceof Orientation);
	}
	
	/**
	 * This method returns the amount of turns required from one to another orientation.
	 * 
	 * @param  fromO
	 * 		   The orientation from which you want to turn
	 * @param  toO
	 * 		   The orientation to which you want to turn
	 * @return The number of turns required to turn from one to another orientation
	 * 		   | if(fromO == TOP && toO == BOTTOM) OR (fromO == BOTTOM && toO == TOP) 
	 * 		   | OR (fromO == LEFT && toO == RIGHT) OR (fromO == RIGHT && toO == LEFT)
	 * 		   | --> result == 2
	 * 		   | else if(fromO == toO)
	 * 		   | --> result == 0
	 * 		   | else
	 * 		   | --> result == 1
	 */
	public static int getAmountOfTurnsRequiredToTurnFromTo(Orientation fromO, Orientation toO){
		int result = 0;
		if(fromO!=null && toO!=null){
			if((fromO == TOP && toO == BOTTOM) ||
					(fromO == BOTTOM && toO == TOP) ||
					(fromO == LEFT && toO == RIGHT) ||
					(fromO == RIGHT && toO == LEFT)){
				result = 2;			
			}
			else if(fromO == toO){
				result = 0;
			}
			else{
				result = 1;
			}
		}
		else{
			System.out.println("One of both orientations is null");
		}
		return result;		
	}
}
